package com.css.bjdt_core.cell.service;

import com.css.bjdt_core.cell.entity.VarParam;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class CustomSqlService {
	@Autowired
	private Dao dao;

	public Sql executeSql(String sqlStr, SqlCallback callback, VarParam... varparams) {
		Sql sql = Sqls.create(sqlStr);
		if (varparams != null) {
			for (VarParam varparam : varparams) {
				String varKey = varparam.getVarKey();
				String varValue = varparam.getVarValue();
				String paramKey = varparam.getParamKey();
				Object paramValue = varparam.getParamValue();
				if (varKey != null && varValue != null) {
					sql.vars().set(varKey, varValue);
				}
				if (paramKey != null && paramValue != null) {
					sql.params().set(paramKey, paramValue);
				}
			}
		}
		if (callback != null) {
			sql.setCallback(callback);
		}
		dao.execute(sql);
		return sql;
	}

	public Sql executeSql(String sqlStr, SqlCallback callback, Map<String, Object> params) {
		Sql sql = Sqls.create(sqlStr);
		if(params != null) {
			sql.params().putAll(params);
		}
		if (callback != null) {
			sql.setCallback(callback);
		}

		dao.execute(sql);
		return sql;
	}

	public Sql executeSql(String sqlStr, SqlCallback callback, Map<String, Object> params, Map<String, Object> vars) {
		Sql sql = Sqls.create(sqlStr);
		if(params != null) {
			sql.params().putAll(params);
		}
		if(vars != null) {
			sql.vars().putAll(vars);
		}
		if (callback != null) {
			sql.setCallback(callback);
		}
		dao.execute(sql);
		return sql;
	}
}
